function [x,y,typ]=FIR_int(job,arg1,arg2)

x=[];y=[];typ=[]
select job
case 'plot' then
  standard_draw(arg1)
case 'getinputs' then
  [x,y,typ]=standard_inputs(arg1)
case 'getoutputs' then
  [x,y,typ]=standard_outputs(arg1)
case 'getorigin' then
  [x,y]=standard_origin(arg1)
case 'set' then
  x=arg1;
  graphics=arg1.graphics;exprs=graphics.exprs
  model=arg1.model;
  x0=model.dstate;ns=prod(size(x0));
  while %t do
    [ok, ft, forder, fl, fh, plot_freq_response, exprs]=getvalue('Set FIR digital filter parameters',..
	['filter type, input 0 for lowpass, 1 for highpass, 2 for bandpass or 3 for bandstop';
	 'filter order',
	 'low cut-off frequency',
	 'high cut-off frequency',
	 'plot frequency response? 0 for no, positive number of yes'
	 ],..
 	list('vec',1,'vec',1,'vec',1,'vec',1, 'vec', 1),exprs)
    if ~ok then break,end
    
    //check validity of parameters
    forder = round(forder);
    if ft ~= int(ft) | ft > 3 | ft < 0 then
      message('filter type must be either 0, 1, 2 or 3')
      ok=%f
    elseif forder < 1
      message('filter order must be a positive integer');
      ok=%f
    elseif fl < 0 | fl > 0.5 | fh < 0 | fh > 0.5
      message('fl and fh must be in the range [0, 0.5]')
      ok = %f;
    elseif fl >= fh
      message('fl must be less than fh');
      ok = %f;
    end
    
    if ok then
      select ft 
        case 0 then
          ftStr = 'lp'
        case 1 then
          ftStr = 'hp'
        case 2 then
          ftStr = 'bp'
        case 3 then
          ftStr = 'sb'
      end
      hn = ffilt(ftStr, forder, fl, fh);
    
      if plot_freq_response ~= 0 then
          message(["impulse response of the filter:" string(hn)])
          win=xget("window")
          xset("window",int(plot_freq_response))
          xset("wdim",600,400)
          xbasc()
          [hm,fr]=frmag(hn,256);
          plot(fr, hm);
          xset("window",win);
      end
      Hz = poly(hn, 'z', 'coeff');
      Hz = horner(Hz, 1/%z);
      H=cont_frm(Hz.num,Hz.den)
      [A,B,C,D]=H(2:5);
      graphics.exprs=exprs;
      [ns1,ns1]=size(A)
      if ns1<=ns then
	x0=x0(1:ns1)
      else
	x0(ns1,1)=0
      end
      rpar=[A(:);
	    B(:);
	    C(:);
	    D(:)]
      model.dstate=x0
      model.rpar=rpar
      if norm(D,1)<>0 then
	mmm=[%t %f];
      else
	mmm=[%f %f];
      end
      if or(model.dep_ut<>mmm) then 
	  model.dep_ut=mmm,end
      x.graphics=graphics;x.model=model
      break
    end
  end
  x.model.firing=[] //compatibility
case 'define' then
  x0=0;
  //A=-1;B=1;C=1;D=0;
  ft = 0;
  ftStr = 'lp';
  forder = 20;
  fl = 0.1;
  fh = 0.5;
  plot_freq_response = 0
  exprs=[string(ft), string(forder), string(fl), string(fh), string(plot_freq_response)];
  hn = ffilt(ftStr, forder, fl, fh);
  Hz = poly(hn, 'z', 'coeff');
  Hz = horner(Hz, 1/%z);
  H=cont_frm(Hz.num,Hz.den)
  [A,B,C,D]=H(2:5);

  model=scicos_model()
  model.sim=list('dsslti4',4)
  model.in=1
  model.out=1
  model.evtin=1
  model.dstate=x0(:)
  model.rpar=[A(:);B(:);C(:);D(:)]
  model.blocktype='d'
  model.dep_ut=[%f %f]

  gr_i=['txt=[''FIR'';''Filter''];';
    'xstringb(orig(1),orig(2),txt,sz(1),sz(2),''fill'');']
  x=standard_define([2.5 2.5],model,exprs,gr_i)
end
endfunction
